package com.xique.door.controller;


import com.xique.common.core.utils.poi.ExcelUtil;
import com.xique.common.core.web.controller.BaseController;
import com.xique.common.core.web.domain.AjaxResult;
import com.xique.common.core.web.page.TableDataInfo;
import com.xique.common.log.annotation.Log;
import com.xique.common.log.enums.BusinessType;
import com.xique.common.security.annotation.PreAuthorize;
import com.xique.door.bean.domain.BuildUnit;
import com.xique.door.service.IBuildUnitService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 单元Controller
 *
 * @author caogq
 * @date 2021-09-09
 */
@Deprecated
@Controller
@RequestMapping("/build/unit")
public class BuildUnitController extends BaseController {
    private String prefix = "community/build/unit";

    @Autowired
    private IBuildUnitService buildUnitService;

    @PreAuthorize(hasPermi = "build:unit:view")
    @GetMapping("/{parkId}")
    public String unit(@PathVariable("parkId") Long parkId, ModelMap mmap) {
        mmap.put("parkId", parkId);
        return prefix + "/unit";
    }

    /**
     * 查询单元列表
     */
    @PreAuthorize(hasPermi = "build:unit:list")
    @PostMapping("/list/{parkId}")
    @ResponseBody
    public TableDataInfo list(BuildUnit buildUnit) {
        startPage();
        List<BuildUnit> list = buildUnitService.selectBuildUnitList(buildUnit);
        return getDataTable(list);
    }

    /**
     * 导出单元列表
     */
    @PreAuthorize(hasPermi = "build:unit:export")
    @PostMapping("/export")
    @ResponseBody
    public void export(HttpServletResponse response, BuildUnit buildUnit) throws IOException {
        List<BuildUnit> list = buildUnitService.selectBuildUnitList(buildUnit);
        ExcelUtil<BuildUnit> util = new ExcelUtil<BuildUnit>(BuildUnit.class);
        util.exportExcel(response, list, "unit");
    }

    /**
     * 新增单元
     */
    @GetMapping("/add/{parkId}")
    public String add(@PathVariable("parkId") Long parkId, ModelMap mmap) {
        mmap.put("parkId", parkId);
        //查出当前车场的楼栋
//        mmap.put("")
        return prefix + "/add";
    }

    /**
     * 新增保存单元
     */
    @PreAuthorize(hasPermi = "build:unit:add")
    @Log(title = "单元", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(BuildUnit buildUnit) {
        BuildUnit search = new BuildUnit();
        search.setCode(buildUnit.getCode());
        if (!buildUnitService.selectBuildUnitList(search).isEmpty()) {
            return AjaxResult.error("编号重复");
        }
        return toAjax(buildUnitService.insertBuildUnit(buildUnit));
    }

    /**
     * 修改单元
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        BuildUnit buildUnit = buildUnitService.selectBuildUnitById(id);
        mmap.put("buildUnit", buildUnit);
        return prefix + "/edit";
    }

    /**
     * 修改保存单元
     */
    @PreAuthorize(hasPermi = "build:unit:edit")
    @Log(title = "单元", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(BuildUnit buildUnit) {
        return toAjax(buildUnitService.updateBuildUnit(buildUnit));
    }

    /**
     * 删除单元
     */
    @PreAuthorize(hasPermi = "build:unit:remove")
    @Log(title = "单元", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(buildUnitService.deleteBuildUnitByIds(ids));
    }
}
